<template>
  <div class="app-container">
    <label class="title"
           style="font-size: 20px;color: rgba(0,0,0,.85);font-weight: 600;line-height: 32px">监控设备</label>
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="关键字" prop="main">
        <el-input
          v-model="queryParams.main"
          placeholder="输入关键字查询"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['business:monitor:add']"
        >添加监控
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-upload"
          size="mini"
          @click="handleUpload"
          v-hasPermi="['business:monitor:upload']"
        >上传文件
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExportModel"
          v-hasPermi="['business:monitor:exportModel']"
        >下载文件模版
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['business:monitor:export']"
        >监控导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="monitorList">
      <el-table-column label="序号" type="index" align="center">
        <template slot-scope="scope">
          <span>{{scope.$index+(queryParams.pageNo-1)*queryParams.pageSize+1}}</span>
        </template>
      </el-table-column>
      <el-table-column label="序列号" align="center" prop="code"/>
      <el-table-column label="验证码" align="center" prop="verify"/>
      <el-table-column label="名称" align="center" prop="name"/>
      <el-table-column label="备注" align="center" prop="remark"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:monitor:edit']"
          >编辑
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:monitor:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNo"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="序列号" prop="code">
          <el-input v-model="form.code" placeholder="请输入"/>
        </el-form-item>
        <el-form-item label="验证码" prop="verify">
          <el-input v-model="form.verify" placeholder="请输入"/>
        </el-form-item>
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入"/>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 文件上传对话框 -->
    <el-dialog title="上传考试设备" :visible.sync="uploadOpen" width="500px" append-to-body>

      <el-col :sm="24" align="middle">
        <el-upload id="importUpload"
                   class="upload-demo"
                   drag
                   :show-file-list=false
                   :on-success="uploadSuccess"
                   :on-error="uploadError"
                   :action="uploadUrl">
          <i class="el-icon-upload"></i>
          <div class="el-upload__text">点击这里或拖放文件到这里上传</div>
        </el-upload>
      </el-col>

      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel2">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    list,
    add,
    edit,
    del,
    upload
  } from '@/api/business/monitor/monitor'

  export default {
    name: 'monitor',
    data() {
      return {
        // 遮罩层
        loading: true,
        // 导出遮罩层
        exportLoading: false,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 表格数据
        monitorList: [],
        // 弹出层标题
        title: '',
        // 是否显示弹出层
        open: false,
        uploadOpen: false,
        // 查询参数
        queryParams: {
          pageNo: 1,
          pageSize: 10,
          main: null
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          code: [
            { required: true, message: '序列号不能为空', trigger: 'blur' }
          ],
          verify: [
            { required: true, message: '验证码不能为空', trigger: 'blur' }
          ],
          name: [
            { required: true, message: '名称不能为空', trigger: 'blur' }
          ]
        },
        uploadUrl: '/' + this.$store.state.settings.prefix + '/business/examMonitors/import'
      }
    },
    created() {
      this.getList()
    },
    methods: {
      /** 查询列表 */
      getList() {
        this.loading = true
        list(this.queryParams).then(response => {
          this.monitorList = response.data.records
          this.total = response.data.totalCount
          this.loading = false
        })
      },
      // 取消按钮
      cancel() {
        this.open = false
        this.reset()
      },
      // 关闭按钮
      cancel2() {
        this.uploadOpen = false
        this.reset()
      },
      // 表单重置
      reset() {
        this.form = {
          code: null,
          name: null,
          verify: null,
          remark: null
        }
        this.resetForm('form')
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1
        this.getList()
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm('queryForm')
        this.handleQuery()
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset()
        this.open = true
        this.title = '添加监控'
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset()
        this.form = row
        this.open = true
        this.title = '修改监控'
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs['form'].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              edit(this.form, this.form.id).then(response => {
                this.$modal.msgSuccess('修改成功')
                this.open = false
                this.getList()
              })
            } else {
              add(this.form).then(response => {
                this.$modal.msgSuccess('新增成功')
                this.open = false
                this.getList()
              })
            }
          }
        })
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids
        this.$modal.confirm('是否确认删除名称为"' + row.name + '"的数据项？').then(function() {
          return del(ids)
        }).then(() => {
          this.getList()
          this.$modal.msgSuccess('删除成功')
        }).catch(() => {
        })
      },
      /** 上传考试设备按钮 */
      handleUpload() {
        this.uploadOpen = true
      },
      /** 下载文件模版 */
      handleExportModel() {
        // this.$router.push('/' + this.$store.state.settings.prefix + '/business/examMonitors/exportModel')
        window.open('/' + this.$store.state.settings.prefix + '/business/examMonitors/exportModel')
      },
      /** 监控文件导出 */
      handleExport() {
        // this.$router.push('/' + this.$store.state.settings.prefix + '/business/examMonitors/export')
        window.open('/' + this.$store.state.settings.prefix + '/business/examMonitors/export')
      },
      //文件上传异常结果
      uploadError(err, file, fileList) {
        this.$modal.msgSuccess('文件导入失败，失败原因：' + err.msg)
      },
      //文件上传成功结果
      uploadSuccess(response, file, fileList) {
        if (response.status != 'success') {
          this.$modal.msgSuccess('文件导入失败,返回原因：' + response.msg)
          return
        }
        this.$modal.msgSuccess('上传成功')
      }
    }
  }
</script>
